﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    [RoutePrefix("api/Doctor")]
    public class DoctorController: ApiController
    {
        public DoctorBll doctorBll { get; set; }

        public ILog log { get; set; }
        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 获取全部医生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetDoctorList")]
        public IHttpActionResult GetDoctorList()
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                PageResult<GroupResult<ContactView>> appointmentList = doctorBll.QryDoctorList(ownerid);
                return Ok(appointmentList);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
         
        }

        /// <summary>
        /// 学术推广首页点击查看详情进入
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetDoctorListByConditions")]
        public IHttpActionResult GetDoctorListByConditions(string liveness,string district,bool isml,DateTime startTime,DateTime endTime,int type,string doctorname)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                Guid new_district = Guid.Empty;
                if (!string.IsNullOrWhiteSpace(district)) {
                    new_district = new Guid(district);
                }
                PageResult<GroupResult<ContactView>> appointmentList = doctorBll.QryDoctorListByConditions(liveness,ownerid, new_district,isml,startTime,endTime,type, doctorname);
                return Ok(appointmentList);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }

        }

        /// <summary>
        /// 根据医生id获取医生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetDoctorById")]
        public IHttpActionResult GetDoctorById(Guid doctorId)
        {
            try
            {
                ContactView contactView = doctorBll.QryDoctorById(doctorId);
                return Ok(contactView);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
           
        }


        /// <summary>
        /// 获取地区信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QryAreaByConditions")]
        public IHttpActionResult QryAreaByConditions()
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                Guid ownerid = new Guid(userInfo.userid.ToString());
                List<ContactView> areas = doctorBll.QryAreaByConditions(ownerid).ToList();
                return Ok(areas);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }


        [HttpPost]
        [Authorize]
        [ActionName("AddDoctor")]
        public IHttpActionResult AddDoctor(ContactInfo contactInfo) {

            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                contactInfo.userid = userInfo.userid;
                //appointmenMolde.new_visitahospital = userInfo.siteid;
                if (contactInfo != null)
                {
                    ResResult resResult = doctorBll.AddDoctor(contactInfo);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

        [HttpPost]
        [Authorize]
        [ActionName("ModDoctor")]
        public IHttpActionResult ModDoctor(ContactInfo contactInfo)
        {

            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                contactInfo.userid = userInfo.userid;
                //appointmenMolde.new_visitahospital = userInfo.siteid;
                if (contactInfo != null)
                {
                    ResResult resResult = doctorBll.ModDoctor(contactInfo);
                    return Ok<ResResult>(resResult);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);

            }
        }

    }
}
